package com.ims.common.utils.http;

import com.alibaba.fastjson.JSONObject;
import javax.xml.namespace.QName;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

public class WebServiceUtil {
    //日志
    private static final Logger logger = LoggerFactory.getLogger(WebServiceUtil.class);
    // 生产地址
    public static final String wsUrl1 = "http://192.168.200.15:8080/uapws/service/ns.bs.jxpro.service.ISaleOrderService?wsdl"  ;
    public static final String wsUrl2 = "http://192.168.200.15:8080/uapws/service/ns.bs.jxpro.service.ISaleinvoiceService?wsdl"  ;
    public static final String wsUrl3 = "http://192.168.200.15:8080/uapws/service/ns.bs.jxpro.service.ICustomer0102Service?wsdl"  ;
    public static final String wsUrl4 = "http://192.168.200.15:8080/uapws/service/ns.bs.jxpro.service.ISaleinvoiceQueryUrlService?wsdl"  ;

    // 测试地址
//    public static final String wsUrl1 = "http://192.168.200.40:8080/uapws/service/ns.bs.jxpro.service.ISaleOrderService?wsdl"  ;
//    public static final String wsUrl2 = "http://192.168.200.40:8080/uapws/service/ns.bs.jxpro.service.ISaleinvoiceService?wsdl"  ;
//    public static final String wsUrl3 = "http://192.168.200.40:8080/uapws/service/ns.bs.jxpro.service.ICustomer0102Service?wsdl"  ;
//    public static final String wsUrl4 = "http://192.168.200.40:8080/uapws/service/ns.bs.jxpro.service.ISaleinvoiceQueryUrlService?wsdl"  ;


    /**
     * 调用webservice接口
     */
    public static String sendWsdl(String wsUrl,  String method, String params) {
        logger.info("--------调用webservice接口begin-------");
        logger.info("地址：{} 方法：{}", wsUrl , method);
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(wsUrl);
        Endpoint endpoint = client.getEndpoint();
        QName qName = new QName(endpoint.getService().getName().getNamespaceURI(), method);
        String json = null;
        try {
            Object[] res = client.invoke(qName, params);
            json = JSONObject.toJSONString(res[0]);
            logger.info("ERP返回数据:" + json.toString());
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("服务器断开连接，请稍后再试,{}",e);
            return ",无法访问ERP接口" + wsUrl + "/" + method + ",异常："+e.getMessage() +",";
        }
        logger.info("--------调用webservice接口end-------");
        return json;


    }
}
